package com.baymax.springboot.cli.services.impl;

import com.baymax.springboot.cli.mapper.HdeAccountMapper;
import com.baymax.springboot.cli.mapper.HdeUserMapper;
import com.baymax.springboot.cli.model.HdeAccount;
import com.baymax.springboot.cli.model.HdeUser;
import com.baymax.springboot.cli.services.ITransactionalSrv;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * @Service是@Component注解的一个特例，作用在类上
 * @Service注解作用域默认为单例
 * 使用注解配置和类路径扫描时，被@Service注解标注的类会被Spring扫描并注册为Bean
 * @Service用于标注服务层组件,表示定义一个bean
 * @Service使用时没有传参数，Bean名称默认为当前类的类名，首字母小写
 * @Service(“serviceBeanId”)或@Service(value=”serviceBeanId”)使用时传参数，使用value作为Bean名字
 */
@Service("TransactionalSrv")
public class TransactionalSrv implements ITransactionalSrv {

    @Resource
    private HdeUserMapper hdeUserMapper;

    @Resource
    private HdeAccountMapper hdeAccountMapper;

    /**
     * 成功插入两条记录的事务
     *
     * 默认等同于 @Transactional(propagation=Propagation.REQUIRED)
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void doTran() {
        LocalDateTime now = LocalDateTime.now();

        HdeUser user = new HdeUser();
        user.setCreator("Huxiao");
        user.setGmtCreated(now);
        user.setModifier("Huxiao");
        user.setGmtModified(now);
        user.setIsDeleted("n");

        user.setAge((byte)26);
        user.setName("tx_test");
        user.setRemark("");
        user.setSalary(new BigDecimal(8000));
        user.setTags("tags");
        user.setYearOfWorking((short)10);
        user.setVersion(1);

        hdeUserMapper.insert(user);


        HdeAccount account = new HdeAccount();
        account.setCreator("Huxiao");
        account.setGmtCreated(now);
        account.setModifier("Huxiao");
        account.setGmtModified(now);
        account.setIsDeleted("n");
        account.setVersion(1);
        account.setPayments(new BigDecimal(5000));
        account.setSn("123");
        account.setStatus("online");
        account.setUserId(user.getId());
        hdeAccountMapper.insert(account);
    }

    /**
     * 期待回滚的事务
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void doTranFail() {
        this.doTran();
        throw new RuntimeException("Rollback!");
    }

    /**
     * 没有事务的新增
     */
    @Override
    public void noTranFail() {
        LocalDateTime now = LocalDateTime.now();

        HdeUser user = new HdeUser();
        user.setCreator("no_tx");
        user.setGmtCreated(now);
        user.setModifier("no_tx");
        user.setGmtModified(now);
        user.setIsDeleted("n");

        user.setAge((byte)26);
        user.setName("no_tx");
        user.setRemark("");
        user.setSalary(new BigDecimal(6000));
        user.setTags("tags");
        user.setYearOfWorking((short)10);
        user.setVersion(1);

        hdeUserMapper.insert(user);


        HdeAccount account = new HdeAccount();
        account.setCreator("no_tx");
        account.setGmtCreated(now);
        account.setModifier("no_tx");
        account.setGmtModified(now);
        account.setIsDeleted("n");
        account.setVersion(1);
        account.setPayments(new BigDecimal(3000));
        account.setSn("321");
        account.setStatus("offline");
        account.setUserId(user.getId());
        hdeAccountMapper.insert(account);
        throw new RuntimeException("Rollback!");
    }
}
